package LeetCode刷题;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2022-01-25 10:22
 * @description: Functions of this class is
 **/
public class 大数相乘 {
    public static long mulitiply(String x, String y) {
        //使用数学方法推到而出
        String x1 = x.substring(0, x.length() / 2);///取前一半
        String x2 = x.substring(x.length() / 2);///取后一半
        String y1 = y.substring(0, y.length() / 2);
        String y2 = y.substring(y.length() / 2);
        int n = x2.length();
        int m = y2.length();
        if (x.length() < 4 && y.length() < 4)  ///位数比较小，直接计算，退出递归
            return (long) Integer.parseInt(x) * Integer.parseInt(y);
        else if (x.length() > 3 && y.length() < 4)///拆解x y不动
            return (long) (mulitiply(x1, y) * Math.pow(10, n) + mulitiply(x2, y));
        else if (x.length() < 4 && y.length() > 3)///x不动 拆y
            return (long) (mulitiply(y1, x) * Math.pow(10, m) + mulitiply(y2, x));
        else
            return (long) (mulitiply(x1, y1) * Math.pow(10, n + m) + mulitiply(x1, y2) * Math.pow(10, n) + mulitiply(x2, y1) * Math.pow(10, m) + mulitiply(x2, y2));
    }

}